package cn.edu.lingnan.controllerV1;

import cn.edu.lingnan.constant;
import cn.edu.lingnan.service.EmailService;
import cn.edu.lingnan.service.UserService;
import cn.edu.lingnan.user.dtos.*;
import cn.edu.lingnan.user.pojos.PageBean;
import cn.edu.lingnan.user.pojos.PageResult;
import cn.edu.lingnan.user.vos.UserInfoVo;
import cn.edu.lingnan.user.vos.UserVO;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private EmailService emailService;

    // 分页查询用户（支持条件查询）
    @PostMapping("/list")
    public Result<PageBean> getUserList(@RequestBody UserQueryDTO queryDTO) {
        log.info("查询用户列表：{}", queryDTO);
        PageBean result = userService.getUserList(queryDTO);
        return Result.success(result);
    }
    //根据id查询用户
    @GetMapping("/findById")
    public Result<UserVO> getUserById(@RequestParam("userId") Integer userId) {
         log.info("查询用户：{}", userId);
         UserVO userVO = userService.getUserById(userId);
         return Result.success(userVO);
    }

    // 删除用户
    @DeleteMapping("/delete")
    public  Result deleteUser(@RequestParam("userId") Integer userId) {
        boolean success = userService.deleteUserById(userId);
        if (!success) {
            return  Result.error(constant.DELETE_FAILURE);
        }
        return Result.success(constant.DELETE_SUCCESS) ;
    }
    //修改用户状态
    @PutMapping("/updateStatus")
    public Result updateStatus(@RequestParam("userId") Integer userId, @RequestParam("status") Integer status) {
        boolean success = userService.updateStatus(userId, status);
        if (!success) {
            return Result.error(constant.UPDATE_FAILURE);
        }
        return Result.success(constant.UPDATE_SUCCESS);
    }

    // 更新用户信息
    @PostMapping("/update")
    public Result updateUser(@RequestBody UpdateUserDTO updateUserDTO) {
        boolean success = userService.updateUser(updateUserDTO);
        return success? Result.success(constant.UPDATE_SUCCESS) : Result.error(constant.UPDATE_FAILURE);
    }
    //新增用户
    @PostMapping("/add")
    public Result addUser(@RequestBody AddUserDto addUserDTO) {
        boolean success = userService.addUser( addUserDTO);
        return success? Result.success(constant.ADD_SUCCESS) : Result.error(constant.ADD_FAILURE);
    }
        // 导出用户信息
    @GetMapping("/export")
    public void exportUsers(HttpServletResponse response,
                            @RequestParam("type") Integer type,
                            @RequestParam("userName") String userName) {
         userService.exportUsers(response, type, userName);
    }


    @GetMapping("/getPassword")
    public Result<UserInfoVo> getPasswordByUserId(@RequestParam String userId) {
        log.info("Request received to get password for user ID: {}", userId);
        UserInfoVo userInfoVo = userService.getPasswordByUserId(userId);
        if (userInfoVo == null) {
            return Result.error("User not found");
        }
        return Result.success(userInfoVo);
    }

    @PostMapping("/updatePassword")
    public Result<String> updatePassword(@RequestBody UpdatePasswordDto updatePasswordDto) {
        log.info("Request received to update password for user ID: {}", updatePasswordDto.getUserId());
        boolean isUpdated = userService.updatePassword(updatePasswordDto);
        if (!isUpdated) {
            return Result.error("Password update failed, incorrect old password or user not found");
        }
        return Result.success("Password updated successfully");
    }



}



